Imports System.Net.Mail
Imports System.Diagnostics.Process
Imports System.Threading

Public Class F_RouteMaster
    Dim WithEvents SBO_Application As SAPbouiCOM.Application
    Dim Ocompany As SAPbobsCOM.Company
    Public ShowFolderBrowserThread As Threading.Thread
    Dim strpath As String
    Sub New(ByVal ocompany1 As SAPbobsCOM.Company, ByVal sbo_application1 As SAPbouiCOM.Application)
        SBO_Application = sbo_application1
        Ocompany = ocompany1
    End Sub
    Public Sub RouteMaster_bind(ByVal oform As SAPbouiCOM.Form)
        Try
            CFL_BP_PetMaster(oform, SBO_Application)
            'CFL_BP_Employee_Mobile(oform, SBO_Application)
            oEdit = oform.Items.Item("4").Specific
            oEdit.String = Format(Now.Date, "dd/MM/yy")
            oEdit = oform.Items.Item("34").Specific
            oEdit.String = Format(Now.Date, "dd/MM/yy")
            'oEdit = oform.Items.Item("36").Specific
            'oEdit.String = "Open"
            oEdit = oform.Items.Item("6").Specific
            oEdit.ChooseFromListUID = "CFLPM"
            oEdit.ChooseFromListAlias = "U_PetCode"
            'oEdit = oform.Items.Item("1000002").Specific
            'oEdit.ChooseFromListUID = "CFLEMPM"
            'oEdit.ChooseFromListAlias = "U_AI_MobileUser"
            oMatrix = oform.Items.Item("48").Specific
            oColumns = oMatrix.Columns
            oItem = oform.Items.Item("1000007")
            oItem.Enabled = False
            oItem = oform.Items.Item("1000008")
            oItem.Enabled = False
            oItem = oform.Items.Item("49")
            oItem.Enabled = False
            oItem = oform.Items.Item("50")
            oItem.Enabled = False
            oItem = oform.Items.Item("51")
            oItem.Enabled = False
            oItem = oform.Items.Item("52")
            oItem.Enabled = False
            oItem = oform.Items.Item("53")
            oItem.Enabled = False
            oEdit = oform.Items.Item("71").Specific
            oEdit.String = Format(DateTime.Now, "ddMMyyyyhhmmssffff")
            oform.DataBrowser.BrowseBy = "32"
        Catch ex As Exception
            SBO_Application.StatusBar.SetText(ex.Message, SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Error)
        End Try
    End Sub
    Public Sub NewTrans_bind_Report(ByVal oform As SAPbouiCOM.Form)
        Try
            CFL_BP_PetMaster(oform, SBO_Application)
            ' CFL_BP_PetMaster1(oform, SBO_Application)
            oform.DataSources.UserDataSources.Add("oedit1", SAPbouiCOM.BoDataType.dt_SHORT_TEXT)
            oform.DataSources.UserDataSources.Add("oedit2", SAPbouiCOM.BoDataType.dt_SHORT_TEXT)
            oform.DataSources.UserDataSources.Add("oedit3", SAPbouiCOM.BoDataType.dt_DATE)
            oform.DataSources.UserDataSources.Add("oedit4", SAPbouiCOM.BoDataType.dt_DATE)
            oform.DataSources.UserDataSources.Add("oedit5", SAPbouiCOM.BoDataType.dt_SHORT_TEXT)
            oEdit = oform.Items.Item("4").Specific
            oEdit.DataBind.SetBound(True, "", "oedit1")
            oEdit.ChooseFromListUID = "CFLPM"
            oEdit.ChooseFromListAlias = "U_PetCode"
            'oEdit = oform.Items.Item("6").Specific
            'oEdit.DataBind.SetBound(True, "", "oedit2")
            'oEdit.ChooseFromListUID = "CFLPM1"
            'oEdit.ChooseFromListAlias = "U_PetCode"
            oEdit = oform.Items.Item("8").Specific
            oEdit.DataBind.SetBound(True, "", "oedit3")
            oEdit.String = Format(Now.Date, "dd/MM/yy")
            oEdit = oform.Items.Item("10").Specific
            oEdit.DataBind.SetBound(True, "", "oedit4")
            oEdit.String = Format(Now.Date, "dd/MM/yy")
            oCombo = oform.Items.Item("12").Specific
            oCombo.DataBind.SetBound(True, "", "oedit5")
            oCombo.ValidValues.Add("All", "A")
            oCombo.ValidValues.Add("Pick Up / Drop Off", "PD")
            oCombo.ValidValues.Add("Pick Up Only", "P")
            oCombo.ValidValues.Add("Drop Off Only", "D")
            oCombo.ValidValues.Add("Boarding", "B")
            oCombo.Select("A", SAPbouiCOM.BoSearchKey.psk_ByDescription)

        Catch ex As Exception
            SBO_Application.StatusBar.SetText(ex.Message, SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Error)
        End Try
    End Sub
    Public Sub Boardind_bind_Report(ByVal oform As SAPbouiCOM.Form)
        Try
            CFL_BP_PetMaster(oform, SBO_Application)
            CFL_BP_PetMaster1(oform, SBO_Application)
            oform.DataSources.UserDataSources.Add("oedit1", SAPbouiCOM.BoDataType.dt_SHORT_TEXT)
            oform.DataSources.UserDataSources.Add("oedit2", SAPbouiCOM.BoDataType.dt_SHORT_TEXT)
            'oform.DataSources.UserDataSources.Add("oedit3", SAPbouiCOM.BoDataType.dt_DATE)
            'oform.DataSources.UserDataSources.Add("oedit4", SAPbouiCOM.BoDataType.dt_DATE)
            'oform.DataSources.UserDataSources.Add("oedit5", SAPbouiCOM.BoDataType.dt_SHORT_TEXT)
            'oEdit = oform.Items.Item("4").Specific
            oEdit.DataBind.SetBound(True, "", "oedit1")
            oEdit.ChooseFromListUID = "CFLPM"
            oEdit.ChooseFromListAlias = "U_PetCode"
            oEdit = oform.Items.Item("6").Specific
            oEdit.DataBind.SetBound(True, "", "oedit2")
            oEdit.ChooseFromListUID = "CFLPM1"
            oEdit.ChooseFromListAlias = "U_PetCode"
            'oEdit = oform.Items.Item("8").Specific
            'oEdit.DataBind.SetBound(True, "", "oedit3")
            'oEdit.String = Format(Now.Date, "dd/MM/yy")
            'oEdit = oform.Items.Item("10").Specific
            'oEdit.DataBind.SetBound(True, "", "oedit4")
            'oEdit.String = Format(Now.Date, "dd/MM/yy")
            'oCombo = oform.Items.Item("12").Specific
            'oCombo.DataBind.SetBound(True, "", "oedit5")
            'oCombo.ValidValues.Add("Pick Up / Drop Off", "PD")
            'oCombo.ValidValues.Add("Pick Up Only", "P")
            'oCombo.ValidValues.Add("Drop Off Only", "D")
            'oCombo.ValidValues.Add("Boarding", "B")

        Catch ex As Exception
            SBO_Application.StatusBar.SetText(ex.Message, SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Error)
        End Try
    End Sub
    Public Sub ShowFolderBrowser()
        ' Try
        Dim MyTest As New OpenFileDialog
        Dim MyProcs() As Process
        MyProcs = Process.GetProcessesByName("SAP Business One")
        Dim i As Integer = 0
        If MyProcs.Length >= 1 Then
            'For i As Integer = 0 To MyProcs.Length - 1
            Dim MyWindow As New WindowWrapper(MyProcs(i).MainWindowHandle)
            MyTest.InitialDirectory = "C:\"
            MyTest.Filter = "All files (*.*)|*.*|All files (*.*)|*.*"
            'MyTest.ShowDialog(MyWindow)

            If MyTest.ShowDialog(MyWindow) = DialogResult.OK Then
                strpath = MyTest.FileName
                'ShowFolderBrowserThread.Abort()
            Else
                'ShowFolderBrowserThread.Abort()
            End If

            ' Next
        Else
            SBO_Application.MessageBox("No SBO instances found.")
            'ShowFolderBrowserThread.Abort()
        End If
        ShowFolderBrowserThread.Abort()
        'Catch ex As Exception
        '    SBO_Application.MessageBox(ex.Message)
        '    ShowFolderBrowserThread.Abort()
        'End Try
    End Sub
    Private Sub SBO_Application_ItemEvent(ByVal FormUID As String, ByRef pVal As SAPbouiCOM.ItemEvent, ByRef BubbleEvent As Boolean) Handles SBO_Application.ItemEvent
        Try
            If pVal.ItemUID = "1" And pVal.Before_Action = False And pVal.InnerEvent = False And pVal.EventType = SAPbouiCOM.BoEventTypes.et_ITEM_PRESSED Then
                If oForm.UniqueID = "AI_ROUTEMASTER" And pVal.FormMode = SAPbouiCOM.BoFormMode.fm_ADD_MODE Then
                    'Add your code here
                    oEdit = oForm.Items.Item("71").Specific
                    oEdit.String = Format(DateTime.Now, "ddMMyyyyhhmmssffff")
                End If
            End If
     
            If pVal.FormUID = "AI_ROUTEMASTER" And pVal.ItemUID = "62" And pVal.EventType = SAPbouiCOM.BoEventTypes.et_ITEM_PRESSED And pVal.Before_Action = False And pVal.InnerEvent = False Then
                oForm = SBO_Application.Forms.Item("AI_ROUTEMASTER")
                oEdit = oForm.Items.Item("61").Specific
                Dim st As String = oEdit.String
                If oEdit.String <> "" Then
                    Loadfile(st)
                End If
            End If
            If pVal.FormUID = "AI_ROUTEMASTER" And pVal.ItemUID = "63" And pVal.EventType = SAPbouiCOM.BoEventTypes.et_ITEM_PRESSED And pVal.Before_Action = False And pVal.InnerEvent = False Then
                Try
                    oForm = SBO_Application.Forms.Item("AI_ROUTEMASTER")

                    ShowFolderBrowserThread = New Threading.Thread(AddressOf ShowFolderBrowser)
                    If ShowFolderBrowserThread.ThreadState = Threading.ThreadState.Unstarted Then
                        ShowFolderBrowserThread.SetApartmentState(Threading.ApartmentState.STA)
                        ShowFolderBrowserThread.Start()
                        ShowFolderBrowserThread.Join()
                    Else
                        ShowFolderBrowserThread.Abort()
                    End If
                    'If pVal.ItemUID = "Button1" And pVal.EventType = SAPbouiCOM.BoEventTypes.et_ITEM_PRESSED Then
                    oEdit = oForm.Items.Item("61").Specific
                    oEdit.String = strpath
                    ShowFolderBrowserThread.Abort()
                    'End If
                Catch ex As Exception

                End Try

            End If

          
            If pVal.FormUID = "BoardingDogsReport" Then
                oForm = SBO_Application.Forms.Item("BoardingDogsReport")
                If pVal.EventType = SAPbouiCOM.BoEventTypes.et_CHOOSE_FROM_LIST Then
                    Dim oCFLEvento As SAPbouiCOM.IChooseFromListEvent
                    oCFLEvento = pVal
                    Dim sCFL_ID As String
                    sCFL_ID = oCFLEvento.ChooseFromListUID
                    oForm = SBO_Application.Forms.Item(FormUID)
                    Dim oCFL As SAPbouiCOM.ChooseFromList
                    oCFL = oForm.ChooseFromLists.Item(sCFL_ID)
                    Try
                        If oCFLEvento.BeforeAction = False Then
                            Dim oDataTable As SAPbouiCOM.DataTable
                            oDataTable = oCFLEvento.SelectedObjects
                            If pVal.ItemUID = "4" Then

                                Try
                                    oEdit = oForm.Items.Item("4").Specific
                                    oEdit.String = oDataTable.GetValue("U_PetCode", 0)
                                Catch ex As Exception
                                End Try
                            End If
                            If pVal.ItemUID = "6" Then
                                'oEdit = oForm.Items.Item("26").Specific
                                'oEdit.String = oDataTable.GetValue("U_U_PetName", 0)
                                Try
                                    oEdit = oForm.Items.Item("6").Specific
                                    oEdit.String = oDataTable.GetValue("U_PetCode", 0)
                                Catch ex As Exception
                                End Try

                            End If
                        End If
                    Catch ex As Exception
                        '' SBO_Application.StatusBar.SetText(ex.Message, SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Error)
                    End Try
                End If
            End If


            'Choose From List

            If pVal.FormUID = "NewTrans" And pVal.ItemUID = "Print" And pVal.Before_Action = True And pVal.EventType = SAPbouiCOM.BoEventTypes.et_ITEM_PRESSED Then
                'DayPassBalanceReport
                Dim trd As Threading.Thread
                trd = New Threading.Thread(AddressOf RoutePlan_Report)
                trd.IsBackground = True
                trd.SetApartmentState(ApartmentState.STA)
                trd.Start()

              
            End If
            '----------------
            If pVal.FormUID = "NewTrans" Then
                oForm = SBO_Application.Forms.Item("NewTrans")
                If pVal.EventType = SAPbouiCOM.BoEventTypes.et_CHOOSE_FROM_LIST Then
                    Dim oCFLEvento As SAPbouiCOM.IChooseFromListEvent
                    oCFLEvento = pVal
                    Dim sCFL_ID As String
                    sCFL_ID = oCFLEvento.ChooseFromListUID
                    oForm = SBO_Application.Forms.Item(FormUID)
                    Dim oCFL As SAPbouiCOM.ChooseFromList
                    oCFL = oForm.ChooseFromLists.Item(sCFL_ID)
                    Try
                        If oCFLEvento.BeforeAction = False Then
                            Dim oDataTable As SAPbouiCOM.DataTable
                            oDataTable = oCFLEvento.SelectedObjects
                            If pVal.ItemUID = "4" Then

                                Try
                                    oEdit = oForm.Items.Item("4").Specific
                                    oEdit.String = oDataTable.GetValue("U_PetCode", 0)
                                Catch ex As Exception
                                End Try
                            End If
                            If pVal.ItemUID = "6" Then
                                'oEdit = oForm.Items.Item("26").Specific
                                'oEdit.String = oDataTable.GetValue("U_U_PetName", 0)
                                Try
                                    oEdit = oForm.Items.Item("6").Specific
                                    oEdit.String = oDataTable.GetValue("U_PetCode", 0)
                                Catch ex As Exception
                                End Try

                            End If
                        End If
                    Catch ex As Exception
                        '' SBO_Application.StatusBar.SetText(ex.Message, SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Error)
                    End Try
                End If
            End If


            'Choose From List
            Try
                If pVal.FormUID = "AI_ROUTEMASTER" Then

                    oForm = SBO_Application.Forms.Item("AI_ROUTEMASTER")
                    If pVal.ItemUID = "RM1000002" And pVal.EventType = SAPbouiCOM.BoEventTypes.et_VALIDATE And pVal.InnerEvent = False And pVal.Before_Action = False Then
                        oEdit = oForm.Items.Item("RM1000002").Specific
                        If oEdit.String <> "" Then
                            Dim Uname As String = MobileUserName(oEdit.String, Ocompany)
                            oEdit = oForm.Items.Item("26").Specific
                            oEdit.String = Uname
                        End If
                    End If
                    If pVal.EventType = SAPbouiCOM.BoEventTypes.et_CHOOSE_FROM_LIST Then
                        Dim oCFLEvento As SAPbouiCOM.IChooseFromListEvent
                        oCFLEvento = pVal
                        Dim sCFL_ID As String
                        sCFL_ID = oCFLEvento.ChooseFromListUID
                        oForm = SBO_Application.Forms.Item(FormUID)
                        Dim oCFL As SAPbouiCOM.ChooseFromList
                        oCFL = oForm.ChooseFromLists.Item(sCFL_ID)
                        Try
                            If oCFLEvento.BeforeAction = False Then
                                Dim oDataTable As SAPbouiCOM.DataTable
                                oDataTable = oCFLEvento.SelectedObjects
                                If pVal.ItemUID = "1000002" Then
                                    oEdit = oForm.Items.Item("26").Specific
                                    oEdit.String = oDataTable.GetValue("firstName", 0)
                                    Try
                                        oEdit = oForm.Items.Item("1000002").Specific
                                        oEdit.String = oDataTable.GetValue("U_AI_MobileUser", 0)
                                    Catch ex As Exception
                                    End Try

                                End If
                                If pVal.ItemUID = "6" Then
                                    oEdit = oForm.Items.Item("8").Specific
                                    oEdit.String = oDataTable.GetValue("U_PetName", 0)
                                    oEdit = oForm.Items.Item("10").Specific
                                    oEdit.String = oDataTable.GetValue("U_AI_CustCode", 0)
                                    oEdit = oForm.Items.Item("12").Specific
                                    oEdit.String = oDataTable.GetValue("U_AI_CustName", 0)
                                    oEdit = oForm.Items.Item("24").Specific
                                    Dim BPCode As String = oDataTable.GetValue("U_AI_CustCode", 0)
                                    oEdit.String = BPAddress(BPCode, Ocompany)
                                    oRecordSet = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                                    oRecordSet.DoQuery("SELECT T0.[Phone1], T0.[Phone2], T0.[Cellular],T0.[Territory] FROM OCRD T0 WHERE T0.[CardCode]  ='" & BPCode & "'")
                                    oEdit = oForm.Items.Item("16").Specific
                                    oEdit.String = oRecordSet.Fields.Item(0).Value
                                    oEdit = oForm.Items.Item("18").Specific
                                    oEdit.String = oRecordSet.Fields.Item(1).Value
                                    oEdit = oForm.Items.Item("20").Specific
                                    oEdit.String = oRecordSet.Fields.Item(2).Value
                                    If oRecordSet.Fields.Item(3).Value = "1" Then
                                        oEdit = oForm.Items.Item("41").Specific
                                        oEdit.String = "CENTRAL"
                                        Try

                                            Dim UserCode As String = "K9002"
                                            oEdit = oForm.Items.Item("26").Specific
                                            oEdit.String = MobileUserName(UserCode, Ocompany)
                                            oEdit = oForm.Items.Item("RM1000002").Specific
                                            oEdit.String = "K9002"
                                        Catch ex As Exception
                                        End Try

                                    ElseIf oRecordSet.Fields.Item(3).Value = "2" Then
                                        oEdit = oForm.Items.Item("41").Specific
                                        oEdit.String = "OUTSKIRTS"
                                        'gopinath
                                        Try

                                            Dim UserCode As String = "K9001"
                                            oEdit = oForm.Items.Item("26").Specific
                                            oEdit.String = MobileUserName(UserCode, Ocompany)
                                            oEdit = oForm.Items.Item("RM1000002").Specific
                                            oEdit.String = "K9001"
                                        Catch ex As Exception
                                        End Try
                                    ElseIf oRecordSet.Fields.Item(3).Value = "3" Then
                                        oEdit = oForm.Items.Item("41").Specific
                                        oEdit.String = "OFFSHORE"
                                        Try

                                            Dim UserCode As String = "K9001"
                                            oEdit = oForm.Items.Item("26").Specific
                                            oEdit.String = MobileUserName(UserCode, Ocompany)
                                            oEdit = oForm.Items.Item("RM1000002").Specific
                                            oEdit.String = "K9001"
                                        Catch ex As Exception
                                        End Try
                                    End If

                                    Try
                                        oEdit = oForm.Items.Item("6").Specific
                                        oEdit.String = oDataTable.GetValue("U_PetCode", 0)
                                    Catch ex As Exception
                                    End Try

                                End If
                            End If
                        Catch ex As Exception
                            '' SBO_Application.StatusBar.SetText(ex.Message, SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Error)
                        End Try
                    End If
                End If
                '--------------------
            Catch ex As Exception
            End Try
            '======================================================
            Try
                '-------------------End Choose From List-----------
                If pVal.FormUID = "AI_ROUTEMASTER" And pVal.InnerEvent = False Then
                    oForm = SBO_Application.Forms.Item("AI_ROUTEMASTER")

                    If pVal.FormMode = SAPbouiCOM.BoFormMode.fm_UPDATE_MODE Then
                        oItem = oForm.Items.Item("1")
                        oItem.Enabled = False
                    Else
                        oItem = oForm.Items.Item("1")
                        oItem.Enabled = True
                    End If
                    If pVal.Before_Action = False Then
                        Select Case pVal.EventType
                            Case SAPbouiCOM.BoEventTypes.et_ITEM_PRESSED
                                If pVal.FormMode = SAPbouiCOM.BoFormMode.fm_ADD_MODE Then

                                    If pVal.ItemUID = "1" Then
                                        oEdit = oForm.Items.Item("4").Specific
                                        oEdit.String = Format(Now.Date, "dd/MM/yy")
                                        oEdit = oForm.Items.Item("34").Specific
                                        oEdit.String = Format(Now.Date, "dd/MM/yy")
                                        oEdit = oForm.Items.Item("36").Specific
                                        oEdit.String = "Open"
                                    End If
                                End If
                            Case SAPbouiCOM.BoEventTypes.et_VALIDATE
                                If pVal.FormMode = SAPbouiCOM.BoFormMode.fm_ADD_MODE Then
                                    If pVal.ItemUID = "47" And pVal.InnerEvent = False And pVal.Before_Action = False Then
                                        oEdit = oForm.Items.Item("47").Specific
                                        oMatrix = oForm.Items.Item("48").Specific
                                        If oEdit.String <> "" Then
                                            Dim Nodays As Integer = oEdit.Value
                                            If Nodays > 0 Then
                                                oMatrix.Clear()
                                                oMatrix.AddRow(Nodays)
                                            End If
                                        End If
                                    ElseIf pVal.ItemUID = "48" And pVal.ColUID = "V_0" And pVal.InnerEvent = False And pVal.Before_Action = False Then
                                        'puni
                                        oMatrix = oForm.Items.Item("48").Specific
                                        oEdit = oMatrix.Columns.Item("V_0").Cells.Item(1).Specific
                                        Dim TrDate As Date = ParamDate(oEdit.String)
                                        Dim i As Integer
                                        For i = 1 To oMatrix.RowCount - 1
                                            oEdit = oMatrix.Columns.Item("V_0").Cells.Item(i + 1).Specific
                                            oEdit.String = TrDate.AddDays(i)
                                        Next
                                    End If
                                End If
                            Case SAPbouiCOM.BoEventTypes.et_COMBO_SELECT
                                If pVal.FormMode = SAPbouiCOM.BoFormMode.fm_ADD_MODE Or pVal.FormMode = SAPbouiCOM.BoFormMode.fm_UPDATE_MODE Then
                                    If pVal.ItemUID = "55" Then
                                        oCombo = oForm.Items.Item("55").Specific
                                        If oCombo.Selected.Value = "Ad-hoc" Then
                                            oItem = oForm.Items.Item("1000007")
                                            oItem.Enabled = False
                                            oItem = oForm.Items.Item("1000008")
                                            oItem.Enabled = False
                                            oItem = oForm.Items.Item("49")
                                            oItem.Enabled = False
                                            oItem = oForm.Items.Item("50")
                                            oItem.Enabled = False
                                            oItem = oForm.Items.Item("51")
                                            oItem.Enabled = False
                                            oItem = oForm.Items.Item("52")
                                            oItem.Enabled = False
                                            oItem = oForm.Items.Item("53")
                                            oItem.Enabled = False
                                            oItem = oForm.Items.Item("48")
                                            oItem.Enabled = True
                                        Else
                                            oItem = oForm.Items.Item("1000007")
                                            oItem.Enabled = True
                                            oItem = oForm.Items.Item("1000008")
                                            oItem.Enabled = True
                                            oItem = oForm.Items.Item("49")
                                            oItem.Enabled = True
                                            oItem = oForm.Items.Item("50")
                                            oItem.Enabled = True
                                            oItem = oForm.Items.Item("51")
                                            oItem.Enabled = True
                                            oItem = oForm.Items.Item("52")
                                            oItem.Enabled = True
                                            oItem = oForm.Items.Item("53")
                                            oItem.Enabled = True
                                            oItem = oForm.Items.Item("48")
                                            oItem.Enabled = False
                                        End If
                                    End If
                                End If
                        End Select
                    ElseIf pVal.Before_Action = True Then
                        Select Case pVal.EventType
                            Case SAPbouiCOM.BoEventTypes.et_ITEM_PRESSED
                                If pVal.FormMode = SAPbouiCOM.BoFormMode.fm_ADD_MODE Or pVal.FormMode = SAPbouiCOM.BoFormMode.fm_UPDATE_MODE Then
                                    If pVal.ItemUID = "68" Then
                                        oMatrix = oForm.Items.Item("48").Specific
                                        oChk1 = oMatrix.Columns.Item("V_1").Cells.Item(1).Specific
                                        Dim i As Integer
                                        For i = 1 To oMatrix.RowCount
                                            oChk1 = oMatrix.Columns.Item("V_1").Cells.Item(i).Specific
                                            oChk1.Checked = True
                                        Next

                                    ElseIf pVal.ItemUID = "69" Then
                                        oMatrix = oForm.Items.Item("48").Specific
                                        oChk1 = oMatrix.Columns.Item("V_1").Cells.Item(1).Specific
                                        Dim i As Integer
                                        For i = 1 To oMatrix.RowCount
                                            oChk1 = oMatrix.Columns.Item("V_1").Cells.Item(i).Specific
                                            oChk1.Checked = False
                                        Next
                                    End If
                                    If pVal.ItemUID = "1" Then
                                        oEdit = oForm.Items.Item("6").Specific
                                        If oEdit.String = "" Then
                                            SBO_Application.StatusBar.SetText("Select Pet Code", SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Error)
                                            BubbleEvent = False
                                            Exit Sub
                                        End If
                                        oEdit = oForm.Items.Item("10").Specific
                                        If oEdit.String = "" Then
                                            SBO_Application.StatusBar.SetText("Select BP/Owner Code", SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Error)
                                            BubbleEvent = False
                                            Exit Sub
                                        End If
                                        oEdit = oForm.Items.Item("34").Specific
                                        If oEdit.String = "" Then
                                            SBO_Application.StatusBar.SetText("Select Document Date", SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Error)
                                            BubbleEvent = False
                                            Exit Sub
                                        End If
                                        oEdit = oForm.Items.Item("4").Specific
                                        If oEdit.String = "" Then
                                            SBO_Application.StatusBar.SetText("Select Transaction Date", SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Error)
                                            BubbleEvent = False
                                            Exit Sub
                                        End If
                                        oEdit = oForm.Items.Item("47").Specific
                                        If oEdit.String = "" Then
                                            SBO_Application.StatusBar.SetText("Select No. of Days", SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Error)
                                            BubbleEvent = False
                                            Exit Sub
                                        End If
                                        If oEdit.Value < 1 Then
                                            SBO_Application.StatusBar.SetText("No. of Days Must be Grater than 0", SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Error)
                                            BubbleEvent = False
                                            Exit Sub
                                        End If
                                        Try
                                            oCombo = oForm.Items.Item("1000004").Specific
                                            If oCombo.Selected.Value = "" Then
                                                SBO_Application.StatusBar.SetText("Select Transaction Type", SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Error)
                                                BubbleEvent = False
                                                Exit Sub
                                            End If

                                        Catch ex As Exception
                                            SBO_Application.StatusBar.SetText("Select Transaction Type", SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Error)
                                            BubbleEvent = False
                                            Exit Sub
                                        End Try
                                        '---
                                        If oCombo.Selected.Value <> "Boarding" Then
                                            oEdit = oForm.Items.Item("RM1000002").Specific
                                            If oEdit.String = "" Then
                                                SBO_Application.StatusBar.SetText("Select Driver Code", SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Error)
                                                BubbleEvent = False
                                                Exit Sub
                                            End If
                                            'oEdit = oForm.Items.Item("39").Specific
                                            'If oEdit.String = "" Then
                                            '    SBO_Application.StatusBar.SetText("Select Priority", SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Error)
                                            '    BubbleEvent = False
                                            '    Exit Sub
                                            'End If

                                        End If

                                        oEdit = oForm.Items.Item("47").Specific
                                        Dim nodays As Integer = oEdit.Value
                                        Dim j As Integer
                                        oCombo = oForm.Items.Item("55").Specific
                                        If oCombo.Selected.Value = "Ad-hoc" Then
                                            For j = 1 To oMatrix.RowCount
                                                oEdit = oMatrix.Columns.Item("V_0").Cells.Item(j).Specific
                                                If oEdit.String = "" Then
                                                    SBO_Application.StatusBar.SetText("Dates Can't Be Empty", SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Error)
                                                    BubbleEvent = False
                                                    Exit Sub
                                                End If
                                            Next
                                        Else
                                            Dim count As Integer = 0
                                            oCheck = oForm.Items.Item("1000007").Specific
                                            If oCheck.Checked = True Then
                                                count = count + 1
                                            End If
                                            oCheck = oForm.Items.Item("1000008").Specific
                                            If oCheck.Checked = True Then
                                                count = count + 1
                                            End If
                                            oCheck = oForm.Items.Item("49").Specific
                                            If oCheck.Checked = True Then
                                                count = count + 1
                                            End If
                                            oCheck = oForm.Items.Item("50").Specific
                                            If oCheck.Checked = True Then
                                                count = count + 1
                                            End If
                                            oCheck = oForm.Items.Item("51").Specific
                                            If oCheck.Checked = True Then
                                                count = count + 1
                                            End If
                                            oCheck = oForm.Items.Item("52").Specific
                                            If oCheck.Checked = True Then
                                                count = count + 1
                                            End If
                                            oCheck = oForm.Items.Item("53").Specific
                                            If oCheck.Checked = True Then
                                                count = count + 1
                                            End If
                                            If count <> nodays Then
                                                SBO_Application.StatusBar.SetText("No.of days and Days Selected Not Tally", SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Error)
                                                BubbleEvent = False
                                                Exit Sub
                                            End If
                                        End If

                                        '       
                                        ' Exit Sub
                                        'DraftSO()
                                        oCombo = oForm.Items.Item("1000004").Specific
                                        If oCombo.Selected.Value = "Boarding" Then
                                            Dim dt As String = Format(Now.Date, "yyyyMMdd")
                                            Try

                                                oRecordSet = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                                                oRecordSet.DoQuery("SELECT count(*) as count,T0.[U_AI_TransType], T0.[U_AI_Date] FROM [dbo].[@AI_BOARDTRANS]  T0 WHERE T0.[U_AI_Date] ='" & dt & "' and  T0.[U_AI_TransType] ='Boarding' GROUP BY T0.[U_AI_TransType], T0.[U_AI_Date]")
                                                Dim k As Integer = oRecordSet.Fields.Item(0).Value
                                                oRecordSet1 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                                                oRecordSet1.DoQuery("SELECT T0.[U_CENTRAL], T0.[U_OUTSKIRTSS], T0.[U_BOARDING] FROM [dbo].[@AI_CENTRALOUTSKIRTS]  T0")
                                                Dim k1 As Integer = oRecordSet1.Fields.Item(2).Value
                                                Dim check As Boolean = False
                                                If k >= k1 Then
                                                    If SBO_Application.MessageBox("Warning! Boarding Dogs Allowed is More Than Permitted  value.Would like to Add?", 2, "OK", "Cancel", "Add") = 3 Then
                                                        check = True
                                                        oRecordSet2 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                                                        Dim str1 As String = "update [dbo].[@AI_CENTRALOUTSKIRTS] set [U_BOARDING]= '" & k & "'"
                                                        oRecordSet2.DoQuery(str1)
                                                    End If
                                                    If check = False Then
                                                        SBO_Application.StatusBar.SetText("Boarding Dogs Allowed is More Than Permitted  value", SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Error)
                                                        BubbleEvent = False
                                                        Exit Sub
                                                    End If
                                                End If

                                            Catch ex As Exception
                                            End Try
                                            '--------------------
                                            If SBO_Application.MessageBox("Would you Like to Send Email to Customer Regarding Boarding Dates?", 1, "Yes", "No") = 1 Then
                                                'Email
                                                Dim sPath As String
                                                sPath = IO.Directory.GetParent(Application.StartupPath).ToString
                                                Dim file As System.IO.StreamReader = New System.IO.StreamReader(sPath & "\K9\" & "Email.txt", True)
                                                Dim Email As String
                                                Email = file.ReadLine()
                                                file.Close()
                                                Dim MyArr As Array
                                                MyArr = Email.Split(";")
                                                Dim SmtpServer As New SmtpClient()
                                                SmtpServer.Credentials = New Net.NetworkCredential(MyArr(0).ToString, MyArr(1).ToString)
                                                SmtpServer.Port = 587
                                                SmtpServer.Host = MyArr(2).ToString
                                                SmtpServer.EnableSsl = True
                                                Dim mail As New MailMessage()
                                                mail = New MailMessage()
                                                oEdit = oForm.Items.Item("10").Specific
                                                oRecordSet2 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                                                Dim str As String = "SELECT T0.[E_Mail] FROM OCRD T0 WHERE T0.[CardCode] ='" & oEdit.String & "'"
                                                oRecordSet2.DoQuery(str)
                                                'Dim addr() As String = oRecordSet2.Fields.Item(0).Value.ToString().Split(",")
                                                Try
                                                    mail.From = New MailAddress(MyArr(0).ToString, "K9 Kulture Lifestyle Pte Ltd", System.Text.Encoding.UTF8)
                                                    mail.To.Add(oRecordSet2.Fields.Item(0).Value.ToString)
                                                    'Dim i As Byte
                                                    'For i = 0 To addr.Length - 1
                                                    '    mail.To.Add(addr(i))
                                                    'Next
                                                    oEdit = oForm.Items.Item("47").Specific
                                                    Dim days As String = oEdit.String
                                                    'oEdit = oForm.Items.Item("45").Specific
                                                    'gopi
                                                    Dim dates As String = ""
                                                    Dim k3 As Integer = 0
                                                    For k3 = 1 To oMatrix.RowCount
                                                        oEdit = oMatrix.Columns.Item("V_0").Cells.Item(k3).Specific
                                                        If k3 = 1 Then
                                                            dates = oEdit.String
                                                        Else
                                                            dates = dates & "," & oEdit.String
                                                        End If

                                                    Next
                                                    If dates <> "" Then
                                                        mail.Subject = "Boarding"
                                                        oEdit = oForm.Items.Item("12").Specific
                                                        Dim CName As String = oEdit.String
                                                        oEdit = oForm.Items.Item("8").Specific
                                                        mail.Body = "Hi  " & CName & ",For Your Pet " & oEdit.String & " No of Boarding Days is " & days & " and Dates are " & dates & ""
                                                        mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure
                                                        mail.ReplyTo = New MailAddress(MyArr(0).ToString)
                                                        SmtpServer.Send(mail)
                                                    End If
                                                Catch ex As Exception
                                                    SBO_Application.MessageBox(ex.ToString())
                                                End Try
                                            End If
                                        Else
                                            Try


                                                oEdit = oForm.Items.Item("41").Specific
                                                If oEdit.String = "CENTRAL" Then
                                                    Dim dt As String = Format(Now.Date, "yyyyMMdd")
                                                    oRecordSet = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                                                    oRecordSet.DoQuery("SELECT count(*), T0.[U_Cen] FROM [dbo].[@AI_BOARDTRANS]  T0 WHERE T0.[U_AI_TransType] <> 'Boarding'  and  T0.[U_Cen] ='Central' and  T0.[U_AI_Date] ='" & dt & "' GROUP BY T0.[U_Cen]")
                                                    Dim k As Integer = oRecordSet.Fields.Item(0).Value
                                                    oRecordSet1 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                                                    oRecordSet1.DoQuery("SELECT T0.[U_CENTRAL], T0.[U_OUTSKIRTSS], T0.[U_BOARDING] FROM [dbo].[@AI_CENTRALOUTSKIRTS]  T0")
                                                    Dim k1 As Integer = oRecordSet1.Fields.Item(0).Value
                                                    Dim check As Boolean = False
                                                    If k >= k1 Then
                                                        If SBO_Application.MessageBox("Warning! Central Dogs Allowed is More Than Permitted  value.Would like to Add?", 2, "OK", "Cancel", "Add") = 3 Then
                                                            check = True
                                                            oRecordSet2 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                                                            Dim str As String = "update [dbo].[@AI_CENTRALOUTSKIRTS] set [U_CENTRAL]= '" & k & "'"
                                                            oRecordSet2.DoQuery(str)
                                                        End If
                                                        If check = False Then
                                                            SBO_Application.StatusBar.SetText("Central Dogs Allowed is More Than Permitted  value", SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Error)
                                                            BubbleEvent = False
                                                            Exit Sub
                                                        End If
                                                    End If
                                                ElseIf oEdit.String = "OUTSKIRTS" Then
                                                    Dim dt As String = Format(Now.Date, "yyyyMMdd")
                                                    oRecordSet = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                                                    oRecordSet.DoQuery("SELECT count(*), T0.[U_Cen] FROM [dbo].[@AI_BOARDTRANS]  T0 WHERE T0.[U_AI_TransType] <> 'Boarding'  and  T0.[U_Cen] ='OUTSKIRTS' and  T0.[U_AI_Date] ='" & dt & "' GROUP BY T0.[U_Cen]")
                                                    Dim k As Integer = oRecordSet.Fields.Item(0).Value
                                                    oRecordSet1 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                                                    oRecordSet1.DoQuery("SELECT T0.[U_CENTRAL], T0.[U_OUTSKIRTSS], T0.[U_BOARDING] FROM [dbo].[@AI_CENTRALOUTSKIRTS]  T0")
                                                    Dim k1 As Integer = oRecordSet1.Fields.Item(1).Value
                                                    Dim check As Boolean = False
                                                    If k >= k1 Then
                                                        If SBO_Application.MessageBox("Warning! OUTSKIRTS Dogs Allowed is More Than Permitted  value.Would like to Add?", 2, "OK", "Cancel", "Add") = 3 Then
                                                            check = True
                                                            oRecordSet2 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                                                            Dim str As String = "update [dbo].[@AI_CENTRALOUTSKIRTS] set [U_OUTSKIRTSS]= '" & k & "'"
                                                            oRecordSet2.DoQuery(str)
                                                        End If
                                                        If check = False Then
                                                            SBO_Application.StatusBar.SetText("OUTSKIRTS Dogs Allowed is More Than Permitted  value", SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Error)
                                                            BubbleEvent = False
                                                            Exit Sub
                                                        End If
                                                    End If

                                                End If
                                            Catch ex As Exception

                                            End Try

                                        End If
                                        Try
                                            oCombo = oForm.Items.Item("1000004").Specific
                                            If oCombo.Selected.Value = "Pick Up / Drop Off" Or oCombo.Selected.Value = "Pick Up Only" Or oCombo.Selected.Value = "Drop Off Only" Then
                                                oEdit = oForm.Items.Item("41").Specific
                                                If oEdit.String <> "CENTRAL" And oEdit.String <> "OUTSKIRTS" And oEdit.String <> "OFFSHORE" Then
                                                    SBO_Application.StatusBar.SetText("Please Enter Correct Value in CENTRAL/OUTSKIRTS/OFFSHORE Field", SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Error)
                                                    BubbleEvent = False
                                                    Exit Sub
                                                End If
                                            End If

                                        Catch ex As Exception

                                        End Try

                                        If pVal.FormMode = SAPbouiCOM.BoFormMode.fm_ADD_MODE Then
                                            DraftSO1()
                                            oCombo = oForm.Items.Item("1000004").Specific
                                            If oCombo.Selected.Value = "Pick Up / Drop Off" Then
                                                RoutePlan_POST("Pick Up Only")
                                                RoutePlan_POST("Drop Off Only")
                                            Else
                                                RoutePlan_POST(oCombo.Selected.Value)
                                            End If
                                        End If
                                    End If
                                End If
                        End Select
                    End If
                End If
            Catch ex As Exception
                '  SBO_Application.StatusBar.SetText(ex.Message, SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Error)
            End Try
        Catch ex As Exception

        End Try

    End Sub
    Public Sub DraftSO1()
        Try
            oCombo = oForm.Items.Item("55").Specific
            Dim type As String = oCombo.Selected.Value
            oCombo = oForm.Items.Item("43").Specific
            Dim NewTrs As String = oCombo.Selected.Value
            If NewTrs = "Yes" And type = "Ad-hoc" Then
                Dim oODRF As SAPbobsCOM.Documents = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oDrafts)
                oODRF.DocObjectCode = SAPbobsCOM.BoObjectTypes.oOrders
                Dim Icode As String
                Dim ccode As String
                oEdit = oForm.Items.Item("10").Specific
                ccode = oEdit.String
                oCombo = oForm.Items.Item("1000004").Specific
                Icode = oCombo.Selected.Value
                oODRF.DocDate = Now.Date
                oODRF.CardCode = ccode
                If Icode = "Boarding" Then
                    oRecordSet = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                    oRecordSet.DoQuery("SELECT T0.[U_AI_BoardRate] FROM [dbo].[@AI_PMATRIXBRD]  T0 WHERE T0.[Code] ='01'")
                    oRecordSet.MoveFirst()
                    Dim up As String = oRecordSet.Fields.Item(0).Value
                    oODRF.Lines.LineTotal = up
                    oODRF.Lines.ItemCode = "BT1002"
                    oEdit = oForm.Items.Item("47").Specific
                    oODRF.Lines.Quantity = oEdit.String
                End If
                If Icode = "Pick Up Only" Or Icode = "Drop Off Only" Then

                    Dim i As Integer
                    '  Dim up As Decimal = 0.0
                    Dim upH As Decimal = 0.0
                    Dim upWD As Decimal = 0.0
                    Dim upWE As Decimal = 0.0

                    '  Dim up As Decimal = 0.0
                    Dim QtyH As Integer = 0
                    Dim QtyWD As Integer = 0
                    Dim QtyWE As Integer = 0


                    For i = 1 To oMatrix.RowCount
                        oEdit = oMatrix.Columns.Item("V_0").Cells.Item(i).Specific
                        Dim dt As String = oEdit.Value
                        Dim dt1 As Date = ParamDate(oEdit.String) ' CDate(oEdit.Value)
                        oRecordSet = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                        Dim yr As String = Now.Date.Year
                        oRecordSet.DoQuery("SELECT T1.[StrDate], T1.[EndDate] FROM OHLD T0  INNER JOIN HLD1 T1 ON T0.HldCode = T1.HldCode WHERE T1.[HldCode] ='" & yr & "' and ( T1.[StrDate] ='" & dt & "' or  T1.[EndDate] ='" & dt & "')")
                        If oRecordSet.RecordCount > 0 Then
                            oRecordSet1 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                            oRecordSet1.DoQuery("SELECT T0.[U_AI_1WayRateC], T0.[U_AI_1WayRateO], T0.[U_AI_1WayRateOF] FROM [dbo].[@AI_PMATRIXTRP]  T0 WHERE T0.[Code] ='08'")
                            oEdit = oForm.Items.Item("41").Specific
                            If oEdit.String = "CENTRAL" Then
                                upH = upH + oRecordSet1.Fields.Item(0).Value
                            ElseIf oEdit.String = "OUTSKIRTS" Then
                                upH = upH + oRecordSet1.Fields.Item(1).Value
                            ElseIf oEdit.String = "OFFSHORE" Then
                                upH = upH + oRecordSet1.Fields.Item(2).Value
                            End If
                            QtyH = QtyH + 1
                        Else
                            Dim code As String = ""
                            If dt1.DayOfWeek = DayOfWeek.Monday Then
                                code = "01"
                            ElseIf dt1.DayOfWeek = DayOfWeek.Tuesday Then
                                code = "02"
                            ElseIf dt1.DayOfWeek = DayOfWeek.Wednesday Then
                                code = "03"
                            ElseIf dt1.DayOfWeek = DayOfWeek.Thursday Then
                                code = "04"
                            ElseIf dt1.DayOfWeek = DayOfWeek.Friday Then
                                code = "05"
                            ElseIf dt1.DayOfWeek = DayOfWeek.Saturday Then
                                code = "06"
                            ElseIf dt1.DayOfWeek = DayOfWeek.Sunday Then
                                code = "07"
                            End If
                            If code = "06" Or code = "07" Then
                                oRecordSet1 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                                oRecordSet1.DoQuery("SELECT T0.[U_AI_1WayRateC], T0.[U_AI_1WayRateO], T0.[U_AI_1WayRateOF] FROM [dbo].[@AI_PMATRIXTRP]  T0 WHERE T0.[Code] ='" & code & "'")
                                oEdit = oForm.Items.Item("41").Specific
                                If oEdit.String = "CENTRAL" Then
                                    upWE = upWE + oRecordSet1.Fields.Item(0).Value
                                ElseIf oEdit.String = "OUTSKIRTS" Then
                                    upWE = upWE + oRecordSet1.Fields.Item(1).Value
                                ElseIf oEdit.String = "OFFSHORE" Then
                                    upWE = upWE + oRecordSet1.Fields.Item(2).Value
                                End If
                                QtyWE = QtyWE + 1
                            Else
                                oRecordSet1 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                                oRecordSet1.DoQuery("SELECT T0.[U_AI_1WayRateC], T0.[U_AI_1WayRateO], T0.[U_AI_1WayRateOF] FROM [dbo].[@AI_PMATRIXTRP]  T0 WHERE T0.[Code] ='" & code & "'")
                                oEdit = oForm.Items.Item("41").Specific
                                If oEdit.String = "CENTRAL" Then
                                    upWD = upWD + oRecordSet1.Fields.Item(0).Value
                                ElseIf oEdit.String = "OUTSKIRTS" Then
                                    upWD = upWD + oRecordSet1.Fields.Item(1).Value
                                ElseIf oEdit.String = "OFFSHORE" Then
                                    upWD = upWD + oRecordSet1.Fields.Item(2).Value
                                End If
                                QtyWD = QtyWD + 1
                            End If
                        End If
                    Next
                    If upH <> 0.0 Then
                        oODRF.Lines.LineTotal = upH
                        oODRF.Lines.ItemCode = "BT1001"
                        oODRF.Lines.Quantity = QtyH
                        oODRF.Lines.Add()
                    End If
                    If upWD <> 0.0 Then
                        oODRF.Lines.LineTotal = upWD
                        oODRF.Lines.ItemCode = "BT1001"
                        oODRF.Lines.Quantity = QtyWD
                        oODRF.Lines.Add()
                    End If
                    If upWE <> 0.0 Then
                        oODRF.Lines.LineTotal = upWE
                        oODRF.Lines.ItemCode = "BT1001"
                        oODRF.Lines.Quantity = QtyWE
                        oODRF.Lines.Add()
                    End If
                    
                End If

                If Icode = "Pick Up / Drop Off" Then

                    Dim i As Integer
                    '  Dim up As Decimal = 0.0
                    Dim upH As Decimal = 0.0
                    Dim upWD As Decimal = 0.0
                    Dim upWE As Decimal = 0.0

                    '  Dim up As Decimal = 0.0
                    Dim QtyH As Integer = 0
                    Dim QtyWD As Integer = 0
                    Dim QtyWE As Integer = 0

                    For i = 1 To oMatrix.RowCount
                        oEdit = oMatrix.Columns.Item("V_0").Cells.Item(i).Specific
                        Dim dt As String = oEdit.Value
                        Dim dt1 As Date = ParamDate(oEdit.String) ' CDate(oEdit.Value)
                        Dim yr As String = Now.Date.Year
                        oRecordSet = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                        oRecordSet.DoQuery("SELECT T1.[StrDate], T1.[EndDate] FROM OHLD T0  INNER JOIN HLD1 T1 ON T0.HldCode = T1.HldCode WHERE T1.[HldCode] ='" & yr & "' and ( T1.[StrDate] ='" & dt & "' or  T1.[EndDate] ='" & dt & "')")
                        If oRecordSet.RecordCount > 0 Then
                            oRecordSet1 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                            oRecordSet1.DoQuery("SELECT T0.[U_AI_2WayRateC], T0.[U_AI_2WayRateO], T0.[U_AI_2WayRateOF] FROM [dbo].[@AI_PMATRIXTRP]  T0 WHERE T0.[Code] ='08'")
                            oEdit = oForm.Items.Item("41").Specific
                            If oEdit.String = "CENTRAL" Then
                                upH = upH + oRecordSet1.Fields.Item(0).Value
                            ElseIf oEdit.String = "OUTSKIRTS" Then
                                upH = upH + oRecordSet1.Fields.Item(1).Value
                            ElseIf oEdit.String = "OFFSHORE" Then
                                upH = upH + oRecordSet1.Fields.Item(2).Value
                            End If
                            QtyH = QtyH + 1
                        Else
                            Dim code As String = ""
                            If dt1.DayOfWeek = DayOfWeek.Monday Then
                                code = "01"
                            ElseIf dt1.DayOfWeek = DayOfWeek.Tuesday Then
                                code = "02"
                            ElseIf dt1.DayOfWeek = DayOfWeek.Wednesday Then
                                code = "03"
                            ElseIf dt1.DayOfWeek = DayOfWeek.Thursday Then
                                code = "04"
                            ElseIf dt1.DayOfWeek = DayOfWeek.Friday Then
                                code = "05"
                            ElseIf dt1.DayOfWeek = DayOfWeek.Saturday Then
                                code = "06"
                            ElseIf dt1.DayOfWeek = DayOfWeek.Sunday Then
                                code = "07"
                            End If
                            If code = "06" Or code = "07" Then
                                oRecordSet1 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                                oRecordSet1.DoQuery("SELECT T0.[U_AI_2WayRateC], T0.[U_AI_2WayRateO], T0.[U_AI_2WayRateOF] FROM [dbo].[@AI_PMATRIXTRP]  T0 WHERE T0.[Code] ='" & code & "'")
                                oEdit = oForm.Items.Item("41").Specific
                                If oEdit.String = "CENTRAL" Then
                                    upWE = upWE + oRecordSet1.Fields.Item(0).Value
                                ElseIf oEdit.String = "OUTSKIRTS" Then
                                    upWE = upWE + oRecordSet1.Fields.Item(1).Value
                                ElseIf oEdit.String = "OFFSHORE" Then
                                    upWE = upWE + oRecordSet1.Fields.Item(2).Value
                                End If
                                QtyWE = QtyWE + 1
                            Else
                                oRecordSet1 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                                oRecordSet1.DoQuery("SELECT T0.[U_AI_2WayRateC], T0.[U_AI_2WayRateO], T0.[U_AI_2WayRateOF] FROM [dbo].[@AI_PMATRIXTRP]  T0 WHERE T0.[Code] ='" & code & "'")
                                oEdit = oForm.Items.Item("41").Specific
                                If oEdit.String = "CENTRAL" Then
                                    upWD = upWD + oRecordSet1.Fields.Item(0).Value
                                ElseIf oEdit.String = "OUTSKIRTS" Then
                                    upWD = upWD + oRecordSet1.Fields.Item(1).Value
                                ElseIf oEdit.String = "OFFSHORE" Then
                                    upWD = upWD + oRecordSet1.Fields.Item(2).Value
                                End If
                                QtyWD = QtyWD + 1
                            End If
                        End If

                    Next
                    If upH <> 0.0 Then
                        oODRF.Lines.LineTotal = upH
                        oODRF.Lines.ItemCode = "BT1009"
                        oODRF.Lines.Quantity = QtyH
                        oODRF.Lines.Add()
                    End If
                    If upWD <> 0.0 Then
                        oODRF.Lines.LineTotal = upWD
                        oODRF.Lines.ItemCode = "BT1009"
                        oODRF.Lines.Quantity = QtyWD
                        oODRF.Lines.Add()
                    End If
                    If upWE <> 0.0 Then
                        oODRF.Lines.LineTotal = upWE
                        oODRF.Lines.ItemCode = "BT1009"
                        oODRF.Lines.Quantity = QtyWE
                        oODRF.Lines.Add()
                    End If
                End If
                'oEdit = oForm.Items.Item("47").Specific
                'If oEdit.String > 0 Then
                '    oODRF.Lines.Quantity = oEdit.Value
                'Else
                '    oODRF.Lines.Quantity = "1"
                'End If
                'oODRF.Lines.Add()
                Dim k As Integer
                k = oODRF.Add()
                Dim SerrorMsg As String = ""
                Ocompany.GetLastError(k, SerrorMsg)
                If k = 0 Then
                    SBO_Application.MessageBox("Draft SO created Successfully!")
                Else
                    SBO_Application.MessageBox("Draft SO created Failed! Error Message: " & SerrorMsg & " ")
                End If
            ElseIf NewTrs = "Yes" And type = "Regular" Then

                Dim oODRF As SAPbobsCOM.Documents = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oDrafts)
                oODRF.DocObjectCode = SAPbobsCOM.BoObjectTypes.oOrders
                Dim Icode As String
                Dim ccode As String
                oEdit = oForm.Items.Item("10").Specific
                ccode = oEdit.String
                oCombo = oForm.Items.Item("1000004").Specific
                Icode = oCombo.Selected.Value
                oODRF.DocDate = Now.Date
                oODRF.CardCode = ccode
                If Icode = "Boarding" Then
                    Dim count As Integer = 0
                    oCheck = oForm.Items.Item("1000007").Specific
                    If oCheck.Checked = True Then
                        count = count + 1
                    End If
                    oCheck = oForm.Items.Item("1000008").Specific
                    If oCheck.Checked = True Then
                        count = count + 1
                    End If
                    oCheck = oForm.Items.Item("49").Specific
                    If oCheck.Checked = True Then
                        count = count + 1
                    End If
                    oCheck = oForm.Items.Item("50").Specific
                    If oCheck.Checked = True Then
                        count = count + 1
                    End If
                    oCheck = oForm.Items.Item("51").Specific
                    If oCheck.Checked = True Then
                        count = count + 1
                    End If
                    oCheck = oForm.Items.Item("52").Specific
                    If oCheck.Checked = True Then
                        count = count + 1
                    End If
                    oCheck = oForm.Items.Item("53").Specific
                    If oCheck.Checked = True Then
                        count = count + 1
                    End If
                    'PGopiath
                    oRecordSet = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                    oRecordSet.DoQuery("SELECT T0.[U_AI_BoardRate] FROM [dbo].[@AI_PMATRIXBRD]  T0 WHERE T0.[Code] ='01'")
                    oRecordSet.MoveFirst()
                    Dim up As String = oRecordSet.Fields.Item(0).Value
                    oODRF.Lines.LineTotal = up * count
                    oODRF.Lines.ItemCode = "BT1002"
                End If
                If Icode = "Pick Up Only" Or Icode = "Drop Off Only" Then
                    Dim i As Integer
                    Dim upH As Decimal = 0.0
                    Dim upWD As Decimal = 0.0
                    Dim upWE As Decimal = 0.0

                    '  Dim up As Decimal = 0.0
                    Dim QtyH As Integer = 0
                    Dim QtyWD As Integer = 0
                    Dim QtyWE As Integer = 0
                    'Dim Mon As String = ""
                    'Dim Tue As String = ""
                    'Dim wed As String = ""
                    'Dim thr As String = ""
                    'Dim fri As String = ""
                    'Dim sat As String = ""
                    'Dim sun As String = ""
                    'Dim count As Integer = 0
                    oCheck = oForm.Items.Item("1000007").Specific
                    If oCheck.Checked = True Then
                        oRecordSet1 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                        oRecordSet1.DoQuery("SELECT T0.[U_AI_1WayRateC], T0.[U_AI_1WayRateO], T0.[U_AI_1WayRateOF] FROM [dbo].[@AI_PMATRIXTRP]  T0 WHERE T0.[Code] ='01'")
                        oEdit = oForm.Items.Item("41").Specific
                        If oEdit.String = "CENTRAL" Then
                            upWD = upWD + oRecordSet1.Fields.Item(0).Value
                        ElseIf oEdit.String = "OUTSKIRTS" Then
                            upWD = upWD + oRecordSet1.Fields.Item(1).Value
                        ElseIf oEdit.String = "OFFSHORE" Then
                            upWD = upWD + oRecordSet1.Fields.Item(2).Value
                        End If
                        QtyWD = QtyWD + 1
                    End If
                    oCheck = oForm.Items.Item("1000008").Specific
                    If oCheck.Checked = True Then
                        oRecordSet1 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                        oRecordSet1.DoQuery("SELECT T0.[U_AI_1WayRateC], T0.[U_AI_1WayRateO], T0.[U_AI_1WayRateOF]  FROM [dbo].[@AI_PMATRIXTRP]  T0 WHERE T0.[Code] ='02'")
                        oEdit = oForm.Items.Item("41").Specific
                        If oEdit.String = "CENTRAL" Then
                            upWD = upWD + oRecordSet1.Fields.Item(0).Value
                        ElseIf oEdit.String = "OUTSKIRTS" Then
                            upWD = upWD + oRecordSet1.Fields.Item(1).Value
                        ElseIf oEdit.String = "OFFSHORE" Then
                            upWD = upWD + oRecordSet1.Fields.Item(2).Value
                        End If
                        QtyWD = QtyWD + 1
                    End If
                    oCheck = oForm.Items.Item("49").Specific
                    If oCheck.Checked = True Then
                        oRecordSet1 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                        oRecordSet1.DoQuery("SELECT T0.[U_AI_1WayRateC], T0.[U_AI_1WayRateO], T0.[U_AI_1WayRateOF] FROM [dbo].[@AI_PMATRIXTRP]  T0 WHERE T0.[Code] ='03'")
                        oEdit = oForm.Items.Item("41").Specific
                        If oEdit.String = "CENTRAL" Then
                            upWD = upWD + oRecordSet1.Fields.Item(0).Value
                        ElseIf oEdit.String = "OUTSKIRTS" Then
                            upWD = upWD + oRecordSet1.Fields.Item(1).Value
                        ElseIf oEdit.String = "OFFSHORE" Then
                            upWD = upWD + oRecordSet1.Fields.Item(2).Value
                        End If
                        QtyWD = QtyWD + 1
                    End If
                    oCheck = oForm.Items.Item("50").Specific
                    If oCheck.Checked = True Then
                        oRecordSet1 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                        oRecordSet1.DoQuery("SELECT T0.[U_AI_1WayRateC], T0.[U_AI_1WayRateO], T0.[U_AI_1WayRateOF] FROM [dbo].[@AI_PMATRIXTRP]  T0 WHERE T0.[Code] ='04'")
                        oEdit = oForm.Items.Item("41").Specific
                        If oEdit.String = "CENTRAL" Then
                            upWD = upWD + oRecordSet1.Fields.Item(0).Value
                        ElseIf oEdit.String = "OUTSKIRTS" Then
                            upWD = upWD + oRecordSet1.Fields.Item(1).Value
                        ElseIf oEdit.String = "OFFSHORE" Then
                            upWD = upWD + oRecordSet1.Fields.Item(2).Value
                        End If
                        QtyWD = QtyWD + 1
                    End If
                    oCheck = oForm.Items.Item("51").Specific
                    If oCheck.Checked = True Then
                        oRecordSet1 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                        oRecordSet1.DoQuery("SELECT T0.[U_AI_1WayRateC], T0.[U_AI_1WayRateO], T0.[U_AI_1WayRateOF] FROM [dbo].[@AI_PMATRIXTRP]  T0 WHERE T0.[Code] ='05'")
                        oEdit = oForm.Items.Item("41").Specific
                        If oEdit.String = "CENTRAL" Then
                            upWD = upWD + oRecordSet1.Fields.Item(0).Value
                        ElseIf oEdit.String = "OUTSKIRTS" Then
                            upWD = upWD + oRecordSet1.Fields.Item(1).Value
                        ElseIf oEdit.String = "OFFSHORE" Then
                            upWD = upWD + oRecordSet1.Fields.Item(2).Value
                        End If
                        QtyWD = QtyWD + 1
                    End If
                    oCheck = oForm.Items.Item("52").Specific
                    If oCheck.Checked = True Then
                        oRecordSet1 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                        oRecordSet1.DoQuery("SELECT T0.[U_AI_1WayRateC], T0.[U_AI_1WayRateO], T0.[U_AI_1WayRateOF] FROM [dbo].[@AI_PMATRIXTRP]  T0 WHERE T0.[Code] ='06'")
                        oEdit = oForm.Items.Item("41").Specific
                        If oEdit.String = "CENTRAL" Then
                            upWE = upWE + oRecordSet1.Fields.Item(0).Value
                        ElseIf oEdit.String = "OUTSKIRTS" Then
                            upWE = upWE + oRecordSet1.Fields.Item(1).Value
                        ElseIf oEdit.String = "OFFSHORE" Then
                            upWE = upWE + oRecordSet1.Fields.Item(2).Value
                        End If
                        QtyWE = QtyWE + 1
                    End If
                    oCheck = oForm.Items.Item("53").Specific
                    If oCheck.Checked = True Then
                        oRecordSet1 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                        oRecordSet1.DoQuery("SELECT T0.[U_AI_1WayRateC], T0.[U_AI_1WayRateO], T0.[U_AI_1WayRateOF] FROM [dbo].[@AI_PMATRIXTRP]  T0 WHERE T0.[Code] ='07'")
                        oEdit = oForm.Items.Item("41").Specific
                        If oEdit.String = "CENTRAL" Then
                            upWE = upWE + oRecordSet1.Fields.Item(0).Value
                        ElseIf oEdit.String = "OUTSKIRTS" Then
                            upWE = upWE + oRecordSet1.Fields.Item(1).Value
                        ElseIf oEdit.String = "OFFSHORE" Then
                            upWE = upWE + oRecordSet1.Fields.Item(2).Value
                        End If
                        QtyWE = QtyWE + 1
                    End If
                    If upWD <> 0.0 Then
                        oODRF.Lines.UnitPrice = upWD
                        oODRF.Lines.ItemCode = "BT1001"
                        oODRF.Lines.Quantity = QtyWD
                        oODRF.Lines.Add()
                    End If
                    If upWE <> 0.0 Then
                        oODRF.Lines.UnitPrice = upWE
                        oODRF.Lines.ItemCode = "BT1001"
                        oODRF.Lines.Quantity = QtyWE
                        oODRF.Lines.Add()
                    End If
                   
                End If

                If Icode = "Pick Up / Drop Off" Then

                    Dim i As Integer
                    Dim upH As Decimal = 0.0
                    Dim upWD As Decimal = 0.0
                    Dim upWE As Decimal = 0.0

                    '  Dim up As Decimal = 0.0
                    Dim QtyH As Integer = 0
                    Dim QtyWD As Integer = 0
                    Dim QtyWE As Integer = 0
                    oCheck = oForm.Items.Item("1000007").Specific
                    If oCheck.Checked = True Then
                        oRecordSet1 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                        oRecordSet1.DoQuery("SELECT T0.[U_AI_2WayRateC], T0.[U_AI_2WayRateO], T0.[U_AI_2WayRateOF] FROM [dbo].[@AI_PMATRIXTRP]  T0 WHERE T0.[Code] ='01'")
                        oEdit = oForm.Items.Item("41").Specific
                        If oEdit.String = "CENTRAL" Then
                            upWD = upWD + oRecordSet1.Fields.Item(0).Value
                        ElseIf oEdit.String = "OUTSKIRTS" Then
                            upWD = upWD + oRecordSet1.Fields.Item(1).Value
                        ElseIf oEdit.String = "OFFSHORE" Then
                            upWD = upWD + oRecordSet1.Fields.Item(2).Value
                        End If
                        QtyWD = QtyWD + 1
                    End If
                    oCheck = oForm.Items.Item("1000008").Specific
                    If oCheck.Checked = True Then
                        oRecordSet1 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                        oRecordSet1.DoQuery("SELECT T0.[U_AI_2WayRateC], T0.[U_AI_2WayRateO], T0.[U_AI_2WayRateOF] FROM [dbo].[@AI_PMATRIXTRP]  T0 WHERE T0.[Code] ='02'")
                        oEdit = oForm.Items.Item("41").Specific
                        If oEdit.String = "CENTRAL" Then
                            upWD = upWD + oRecordSet1.Fields.Item(0).Value
                        ElseIf oEdit.String = "OUTSKIRTS" Then
                            upWD = upWD + oRecordSet1.Fields.Item(1).Value
                        ElseIf oEdit.String = "OFFSHORE" Then
                            upWD = upWD + oRecordSet1.Fields.Item(2).Value
                        End If
                        QtyWD = QtyWD + 1
                    End If
                    oCheck = oForm.Items.Item("49").Specific
                    If oCheck.Checked = True Then
                        oRecordSet1 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                        oRecordSet1.DoQuery("SELECT T0.[U_AI_2WayRateC], T0.[U_AI_2WayRateO], T0.[U_AI_2WayRateOF] FROM [dbo].[@AI_PMATRIXTRP]  T0 WHERE T0.[Code] ='03'")
                        oEdit = oForm.Items.Item("41").Specific
                        If oEdit.String = "CENTRAL" Then
                            upWD = upWD + oRecordSet1.Fields.Item(0).Value
                        ElseIf oEdit.String = "OUTSKIRTS" Then
                            upWD = upWD + oRecordSet1.Fields.Item(1).Value
                        ElseIf oEdit.String = "OFFSHORE" Then
                            upWD = upWD + oRecordSet1.Fields.Item(2).Value
                        End If
                        QtyWD = QtyWD + 1
                    End If
                    oCheck = oForm.Items.Item("50").Specific
                    If oCheck.Checked = True Then
                        oRecordSet1 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                        oRecordSet1.DoQuery("SELECT T0.[U_AI_2WayRateC], T0.[U_AI_2WayRateO], T0.[U_AI_2WayRateOF] FROM [dbo].[@AI_PMATRIXTRP]  T0 WHERE T0.[Code] ='04'")
                        oEdit = oForm.Items.Item("41").Specific
                        If oEdit.String = "CENTRAL" Then
                            upWD = upWD + oRecordSet1.Fields.Item(0).Value
                        ElseIf oEdit.String = "OUTSKIRTS" Then
                            upWD = upWD + oRecordSet1.Fields.Item(1).Value
                        ElseIf oEdit.String = "OFFSHORE" Then
                            upWD = upWD + oRecordSet1.Fields.Item(2).Value
                        End If
                        QtyWD = QtyWD + 1
                    End If
                    oCheck = oForm.Items.Item("51").Specific
                    If oCheck.Checked = True Then
                        oRecordSet1 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                        oRecordSet1.DoQuery("SELECT T0.[U_AI_2WayRateC], T0.[U_AI_2WayRateO], T0.[U_AI_2WayRateOF] FROM [dbo].[@AI_PMATRIXTRP]  T0 WHERE T0.[Code] ='05'")
                        oEdit = oForm.Items.Item("41").Specific
                        If oEdit.String = "CENTRAL" Then
                            upWD = upWD + oRecordSet1.Fields.Item(0).Value
                        ElseIf oEdit.String = "OUTSKIRTS" Then
                            upWD = upWD + oRecordSet1.Fields.Item(1).Value
                        ElseIf oEdit.String = "OFFSHORE" Then
                            upWD = upWD + oRecordSet1.Fields.Item(2).Value
                        End If
                        QtyWD = QtyWD + 1
                    End If
                    oCheck = oForm.Items.Item("52").Specific
                    If oCheck.Checked = True Then
                        oRecordSet1 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                        oRecordSet1.DoQuery("SELECT T0.[U_AI_2WayRateC], T0.[U_AI_2WayRateO], T0.[U_AI_2WayRateOF] FROM [dbo].[@AI_PMATRIXTRP]  T0 WHERE T0.[Code] ='06'")
                        oEdit = oForm.Items.Item("41").Specific
                        If oEdit.String = "CENTRAL" Then
                            upWE = upWE + oRecordSet1.Fields.Item(0).Value
                        ElseIf oEdit.String = "OUTSKIRTS" Then
                            upWE = upWE + oRecordSet1.Fields.Item(1).Value
                        ElseIf oEdit.String = "OFFSHORE" Then
                            upWE = upWE + oRecordSet1.Fields.Item(2).Value
                        End If
                        QtyWE = QtyWE + 1
                    End If
                    oCheck = oForm.Items.Item("53").Specific
                    If oCheck.Checked = True Then
                        oRecordSet1 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                        oRecordSet1.DoQuery("SELECT T0.[U_AI_2WayRateC], T0.[U_AI_2WayRateO], T0.[U_AI_2WayRateOF] FROM [dbo].[@AI_PMATRIXTRP]  T0 WHERE T0.[Code] ='07'")
                        oEdit = oForm.Items.Item("41").Specific
                        If oEdit.String = "CENTRAL" Then
                            upWE = upWE + oRecordSet1.Fields.Item(0).Value
                        ElseIf oEdit.String = "OUTSKIRTS" Then
                            upWE = upWE + oRecordSet1.Fields.Item(1).Value
                        ElseIf oEdit.String = "OFFSHORE" Then
                            upWE = upWE + oRecordSet1.Fields.Item(2).Value
                        End If
                        QtyWE = QtyWE + 1
                    End If
                    If upWD <> 0.0 Then
                        oODRF.Lines.UnitPrice = upWD
                        oODRF.Lines.ItemCode = "BT1009"
                        oODRF.Lines.Quantity = QtyWD * 52
                        oODRF.Lines.Add()
                    End If
                    If upWE <> 0.0 Then
                        oODRF.Lines.UnitPrice = upWE
                        oODRF.Lines.ItemCode = "BT1009"
                        oODRF.Lines.Quantity = QtyWE * 52
                        oODRF.Lines.Add()
                    End If
                End If
                'oEdit = oForm.Items.Item("47").Specific
                'If oEdit.String > 0 Then
                '    oODRF.Lines.Quantity = (oEdit.Value * 52)
                'Else
                '    oODRF.Lines.Quantity = "1"
                'End If
                oODRF.Lines.Add()
                Dim k As Integer
                k = oODRF.Add()
                Dim SerrorMsg As String = ""
                Ocompany.GetLastError(k, SerrorMsg)
                If k = 0 Then
                    SBO_Application.MessageBox("Draft SO created Successfully!")
                Else
                    SBO_Application.MessageBox("Draft SO created Failed! Error Message: " & SerrorMsg & " ")
                End If

            End If




        Catch ex As Exception

        End Try
    End Sub
    Function ParamDate(ByVal dt As String) As Date
        Dim d, m, y As String
        Dim Z As Date
        d = dt.Substring(0, 2)
        m = dt.Substring(3, 2)
        y = dt.Substring(6, 2)
        'Z = CDate("May 25, 1986")
        Z = CDate(d & "/" & m & "/" & y)
        'Return System.DateTime.Parse(dt, format1, Globalization.DateTimeStyles.None)
        Return Z
    End Function
  


    Public Sub RoutePlan_POST(ByVal TrType As String)
        oMatrix = oForm.Items.Item("48").Specific
        Try
            Dim i As Integer
            oCombo = oForm.Items.Item("55").Specific
            If oCombo.Selected.Value = "Ad-hoc" Then
                For i = 1 To oMatrix.RowCount
                    oChk1 = oMatrix.Columns.Item("V_1").Cells.Item(i).Specific
                    If oChk1.Checked = True Then
                        Dim oGeneralService As SAPbobsCOM.GeneralService
                        Dim oGeneralData As SAPbobsCOM.GeneralData
                        Dim oSons As SAPbobsCOM.GeneralDataCollection
                        Dim oSon As SAPbobsCOM.GeneralData
                        Dim sCmp As SAPbobsCOM.CompanyService
                        sCmp = Ocompany.GetCompanyService
                        oGeneralService = sCmp.GetGeneralService("AI_BOARDTRANS")
                        oGeneralData = oGeneralService.GetDataInterface(SAPbobsCOM.GeneralServiceDataInterfaces.gsGeneralData)
                        oGeneralData.SetProperty("U_DocDate", Now.Date)
                        oEdit = oForm.Items.Item("71").Specific
                        oGeneralData.SetProperty("U_Ref1", oEdit.String)
                        oEdit = oForm.Items.Item("6").Specific
                        oGeneralData.SetProperty("U_AI_PetCode", oEdit.String)
                        oEdit = oForm.Items.Item("8").Specific
                        oGeneralData.SetProperty("U_AI_PetName", oEdit.String)
                        oEdit = oForm.Items.Item("10").Specific
                        oGeneralData.SetProperty("U_AI_CustCode", oEdit.String)
                        oEdit = oForm.Items.Item("12").Specific
                        oGeneralData.SetProperty("U_AI_CustName", oEdit.String)
                        oEdit = oForm.Items.Item("16").Specific
                        oGeneralData.SetProperty("U_AI_Phone1", oEdit.String)
                        oEdit = oForm.Items.Item("18").Specific
                        oGeneralData.SetProperty("U_AI_Phone2", oEdit.String)
                        oEdit = oForm.Items.Item("20").Specific
                        oGeneralData.SetProperty("U_AI_Mobile", oEdit.String)
                        oEdit = oForm.Items.Item("24").Specific
                        oGeneralData.SetProperty("U_AI_Address", oEdit.String)
                        oEdit = oForm.Items.Item("RM1000002").Specific
                        oGeneralData.SetProperty("U_AI_DriverCode", oEdit.String)
                        oEdit = oForm.Items.Item("26").Specific
                        oGeneralData.SetProperty("U_AI_DriverName", oEdit.String)
                        oEdit = oForm.Items.Item("39").Specific
                        oGeneralData.SetProperty("U_AI_Priority", oEdit.String)
                        ' oCombo = oForm.Items.Item("1000004").Specific
                        oGeneralData.SetProperty("U_AI_TransType", TrType)
                        oCombo = oForm.Items.Item("1000006").Specific
                        oGeneralData.SetProperty("U_Emg", oCombo.Selected.Value)
                        oEdit = oForm.Items.Item("41").Specific
                        oGeneralData.SetProperty("U_Cen", oEdit.String)
                        oEdit = oForm.Items.Item("47").Specific
                        oGeneralData.SetProperty("U_NoNit", oEdit.String)
                        oCombo = oForm.Items.Item("43").Specific
                        oGeneralData.SetProperty("U_AI_NewBTD", oCombo.Selected.Value)
                        oEdit = oMatrix.Columns.Item("V_0").Cells.Item(i).Specific
                        Dim TrDate As Date = ParamDate(oEdit.String)
                        oGeneralData.SetProperty("U_AI_Date", TrDate)
                        oGeneralService.Add(oGeneralData)
                    End If
                Next
            ElseIf oCombo.Selected.Value = "Regular" Then
                oCheck = oForm.Items.Item("1000007").Specific
                If oCheck.Checked = True Then
                    Dim dt1 As Date = Now.Date
                    Dim dt2 As Date
                    If dt1.DayOfWeek = DayOfWeek.Monday Then
                        dt2 = dt1
                    ElseIf dt1.AddDays(1).DayOfWeek = DayOfWeek.Monday Then
                        dt2 = dt1.AddDays(1)
                    ElseIf dt1.AddDays(2).DayOfWeek = DayOfWeek.Monday Then
                        dt2 = dt1.AddDays(2)
                    ElseIf dt1.AddDays(3).DayOfWeek = DayOfWeek.Monday Then
                        dt2 = dt1.AddDays(3)
                    ElseIf dt1.AddDays(4).DayOfWeek = DayOfWeek.Monday Then
                        dt2 = dt1.AddDays(4)
                    ElseIf dt1.AddDays(5).DayOfWeek = DayOfWeek.Monday Then
                        dt2 = dt1.AddDays(5)
                    ElseIf dt1.AddDays(6).DayOfWeek = DayOfWeek.Monday Then
                        dt2 = dt1.AddDays(6)
                    ElseIf dt1.AddDays(7).DayOfWeek = DayOfWeek.Monday Then
                        dt2 = dt1.AddDays(7)
                    End If
                    DAYS(dt2, TrType)
                End If
                oCheck = oForm.Items.Item("1000008").Specific
                If oCheck.Checked = True Then
                    Dim dt1 As Date = Now.Date
                    Dim dt2 As Date
                    If dt1.DayOfWeek = DayOfWeek.Tuesday Then
                        dt2 = dt1
                    ElseIf dt1.AddDays(1).DayOfWeek = DayOfWeek.Tuesday Then
                        dt2 = dt1.AddDays(1)
                    ElseIf dt1.AddDays(2).DayOfWeek = DayOfWeek.Tuesday Then
                        dt2 = dt1.AddDays(2)
                    ElseIf dt1.AddDays(3).DayOfWeek = DayOfWeek.Tuesday Then
                        dt2 = dt1.AddDays(3)
                    ElseIf dt1.AddDays(4).DayOfWeek = DayOfWeek.Tuesday Then
                        dt2 = dt1.AddDays(4)
                    ElseIf dt1.AddDays(5).DayOfWeek = DayOfWeek.Tuesday Then
                        dt2 = dt1.AddDays(5)
                    ElseIf dt1.AddDays(6).DayOfWeek = DayOfWeek.Tuesday Then
                        dt2 = dt1.AddDays(6)
                    ElseIf dt1.AddDays(7).DayOfWeek = DayOfWeek.Tuesday Then
                        dt2 = dt1.AddDays(7)
                    End If
                    DAYS(dt2, TrType)
                End If
                oCheck = oForm.Items.Item("49").Specific
                If oCheck.Checked = True Then
                    Dim dt1 As Date = Now.Date
                    Dim dt2 As Date
                    If dt1.DayOfWeek = DayOfWeek.Wednesday Then
                        dt2 = dt1
                    ElseIf dt1.AddDays(1).DayOfWeek = DayOfWeek.Wednesday Then
                        dt2 = dt1.AddDays(1)
                    ElseIf dt1.AddDays(2).DayOfWeek = DayOfWeek.Wednesday Then
                        dt2 = dt1.AddDays(2)
                    ElseIf dt1.AddDays(3).DayOfWeek = DayOfWeek.Wednesday Then
                        dt2 = dt1.AddDays(3)
                    ElseIf dt1.AddDays(4).DayOfWeek = DayOfWeek.Wednesday Then
                        dt2 = dt1.AddDays(4)
                    ElseIf dt1.AddDays(5).DayOfWeek = DayOfWeek.Wednesday Then
                        dt2 = dt1.AddDays(5)
                    ElseIf dt1.AddDays(6).DayOfWeek = DayOfWeek.Wednesday Then
                        dt2 = dt1.AddDays(6)
                    ElseIf dt1.AddDays(7).DayOfWeek = DayOfWeek.Wednesday Then
                        dt2 = dt1.AddDays(7)
                    End If
                    DAYS(dt2, TrType)
                End If
                oCheck = oForm.Items.Item("50").Specific
                If oCheck.Checked = True Then
                    Dim dt1 As Date = Now.Date
                    Dim dt2 As Date
                    If dt1.DayOfWeek = DayOfWeek.Thursday Then
                        dt2 = dt1
                    ElseIf dt1.AddDays(1).DayOfWeek = DayOfWeek.Thursday Then
                        dt2 = dt1.AddDays(1)
                    ElseIf dt1.AddDays(2).DayOfWeek = DayOfWeek.Thursday Then
                        dt2 = dt1.AddDays(2)
                    ElseIf dt1.AddDays(3).DayOfWeek = DayOfWeek.Thursday Then
                        dt2 = dt1.AddDays(3)
                    ElseIf dt1.AddDays(4).DayOfWeek = DayOfWeek.Thursday Then
                        dt2 = dt1.AddDays(4)
                    ElseIf dt1.AddDays(5).DayOfWeek = DayOfWeek.Thursday Then
                        dt2 = dt1.AddDays(5)
                    ElseIf dt1.AddDays(6).DayOfWeek = DayOfWeek.Thursday Then
                        dt2 = dt1.AddDays(6)
                    ElseIf dt1.AddDays(7).DayOfWeek = DayOfWeek.Thursday Then
                        dt2 = dt1.AddDays(7)
                    End If
                    DAYS(dt2, TrType)
                End If
                oCheck = oForm.Items.Item("51").Specific
                If oCheck.Checked = True Then
                    Dim dt1 As Date = Now.Date
                    Dim dt2 As Date
                    If dt1.DayOfWeek = DayOfWeek.Friday Then
                        dt2 = dt1
                    ElseIf dt1.AddDays(1).DayOfWeek = DayOfWeek.Friday Then
                        dt2 = dt1.AddDays(1)
                    ElseIf dt1.AddDays(2).DayOfWeek = DayOfWeek.Friday Then
                        dt2 = dt1.AddDays(2)
                    ElseIf dt1.AddDays(3).DayOfWeek = DayOfWeek.Friday Then
                        dt2 = dt1.AddDays(3)
                    ElseIf dt1.AddDays(4).DayOfWeek = DayOfWeek.Friday Then
                        dt2 = dt1.AddDays(4)
                    ElseIf dt1.AddDays(5).DayOfWeek = DayOfWeek.Friday Then
                        dt2 = dt1.AddDays(5)
                    ElseIf dt1.AddDays(6).DayOfWeek = DayOfWeek.Friday Then
                        dt2 = dt1.AddDays(6)
                    ElseIf dt1.AddDays(7).DayOfWeek = DayOfWeek.Friday Then
                        dt2 = dt1.AddDays(7)
                    End If
                    DAYS(dt2, TrType)
                End If
                oCheck = oForm.Items.Item("52").Specific
                If oCheck.Checked = True Then
                    Dim dt1 As Date = Now.Date
                    Dim dt2 As Date
                    If dt1.DayOfWeek = DayOfWeek.Saturday Then
                        dt2 = dt1
                    ElseIf dt1.AddDays(1).DayOfWeek = DayOfWeek.Saturday Then
                        dt2 = dt1.AddDays(1)
                    ElseIf dt1.AddDays(2).DayOfWeek = DayOfWeek.Saturday Then
                        dt2 = dt1.AddDays(2)
                    ElseIf dt1.AddDays(3).DayOfWeek = DayOfWeek.Saturday Then
                        dt2 = dt1.AddDays(3)
                    ElseIf dt1.AddDays(4).DayOfWeek = DayOfWeek.Saturday Then
                        dt2 = dt1.AddDays(4)
                    ElseIf dt1.AddDays(5).DayOfWeek = DayOfWeek.Saturday Then
                        dt2 = dt1.AddDays(5)
                    ElseIf dt1.AddDays(6).DayOfWeek = DayOfWeek.Saturday Then
                        dt2 = dt1.AddDays(6)
                    ElseIf dt1.AddDays(7).DayOfWeek = DayOfWeek.Saturday Then
                        dt2 = dt1.AddDays(7)
                    End If
                    DAYS(dt2, TrType)
                End If
                oCheck = oForm.Items.Item("53").Specific
                If oCheck.Checked = True Then
                    Dim dt1 As Date = Now.Date
                    Dim dt2 As Date
                    If dt1.DayOfWeek = DayOfWeek.Sunday Then
                        dt2 = dt1
                    ElseIf dt1.AddDays(1).DayOfWeek = DayOfWeek.Sunday Then
                        dt2 = dt1.AddDays(1)
                    ElseIf dt1.AddDays(2).DayOfWeek = DayOfWeek.Sunday Then
                        dt2 = dt1.AddDays(2)
                    ElseIf dt1.AddDays(3).DayOfWeek = DayOfWeek.Sunday Then
                        dt2 = dt1.AddDays(3)
                    ElseIf dt1.AddDays(4).DayOfWeek = DayOfWeek.Sunday Then
                        dt2 = dt1.AddDays(4)
                    ElseIf dt1.AddDays(5).DayOfWeek = DayOfWeek.Sunday Then
                        dt2 = dt1.AddDays(5)
                    ElseIf dt1.AddDays(6).DayOfWeek = DayOfWeek.Sunday Then
                        dt2 = dt1.AddDays(6)
                    ElseIf dt1.AddDays(7).DayOfWeek = DayOfWeek.Sunday Then
                        dt2 = dt1.AddDays(7)
                    End If
                    DAYS(dt2, TrType)
                End If
            End If
        Catch ex As Exception

        End Try


    End Sub
    Public Sub DAYS(ByVal dt2 As Date, ByVal TrType As String)
        Try
            Dim nodays As Integer = 52
            Dim i As Integer
            For i = 1 To nodays
                If i = 1 Then
                    dt2 = dt2
                Else
                    dt2 = dt2.AddDays(7)
                End If


                Dim oGeneralService As SAPbobsCOM.GeneralService
                Dim oGeneralData As SAPbobsCOM.GeneralData
                Dim oSons As SAPbobsCOM.GeneralDataCollection
                Dim oSon As SAPbobsCOM.GeneralData
                Dim sCmp As SAPbobsCOM.CompanyService
                sCmp = Ocompany.GetCompanyService
                oGeneralService = sCmp.GetGeneralService("AI_BOARDTRANS")
                oGeneralData = oGeneralService.GetDataInterface(SAPbobsCOM.GeneralServiceDataInterfaces.gsGeneralData)
                oGeneralData.SetProperty("U_DocDate", Now.Date)
                oEdit = oForm.Items.Item("6").Specific
                oEdit = oForm.Items.Item("71").Specific
                oGeneralData.SetProperty("U_Ref1", oEdit.String)
                oGeneralData.SetProperty("U_AI_PetCode", oEdit.String)
                oEdit = oForm.Items.Item("8").Specific
                oGeneralData.SetProperty("U_AI_PetName", oEdit.String)
                oEdit = oForm.Items.Item("10").Specific
                oGeneralData.SetProperty("U_AI_CustCode", oEdit.String)
                oEdit = oForm.Items.Item("12").Specific
                oGeneralData.SetProperty("U_AI_CustName", oEdit.String)
                oEdit = oForm.Items.Item("16").Specific
                oGeneralData.SetProperty("U_AI_Phone1", oEdit.String)
                oEdit = oForm.Items.Item("18").Specific
                oGeneralData.SetProperty("U_AI_Phone2", oEdit.String)
                oEdit = oForm.Items.Item("20").Specific
                oGeneralData.SetProperty("U_AI_Mobile", oEdit.String)
                oEdit = oForm.Items.Item("24").Specific
                oGeneralData.SetProperty("U_AI_Address", oEdit.String)
                oEdit = oForm.Items.Item("RM1000002").Specific
                oGeneralData.SetProperty("U_AI_DriverCode", oEdit.String)
                oEdit = oForm.Items.Item("26").Specific
                oGeneralData.SetProperty("U_AI_DriverName", oEdit.String)
                oEdit = oForm.Items.Item("39").Specific
                oGeneralData.SetProperty("U_AI_Priority", oEdit.String)
                'oCombo = oForm.Items.Item("1000004").Specific
                oGeneralData.SetProperty("U_AI_TransType", TrType)
                oCombo = oForm.Items.Item("1000006").Specific
                oGeneralData.SetProperty("U_Emg", oCombo.Selected.Value)
                oEdit = oForm.Items.Item("41").Specific
                oGeneralData.SetProperty("U_Cen", oEdit.String)
                oEdit = oForm.Items.Item("47").Specific
                oGeneralData.SetProperty("U_NoNit", oEdit.String)
                oCombo = oForm.Items.Item("43").Specific
                oGeneralData.SetProperty("U_AI_NewBTD", oCombo.Selected.Value)
                oGeneralData.SetProperty("U_AI_Date", dt2)
                oGeneralService.Add(oGeneralData)
            Next

        Catch ex As Exception

        End Try
    End Sub

    Private Sub SBO_Application_MenuEvent(ByRef pVal As SAPbouiCOM.MenuEvent, ByRef BubbleEvent As Boolean) Handles SBO_Application.MenuEvent
        Try
            If pVal.MenuUID = "1283" And pVal.BeforeAction = True And pVal.InnerEvent = False Then
                oForm = SBO_Application.Forms.ActiveForm
                If oForm.UniqueID = "AI_ROUTEMASTER" Then
                    Try

                        oForm = SBO_Application.Forms.Item("AI_ROUTEMASTER")
                        oEdit = oForm.Items.Item("71").Specific
                        Dim refno As String = oEdit.String
                        Dim dt As String = Format(Now.Date, "yyyyMMdd")
                        oRecordSet = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                        oRecordSet.DoQuery("delete  FROM [dbo].[@AI_BOARDTRANS]   WHERE [U_Ref1] ='" & oEdit.String & "' and U_AI_Date >='" & dt & "'")

                    Catch ex As Exception

                    End Try

                End If
            End If
            If pVal.MenuUID = "1282" And pVal.InnerEvent = False And pVal.BeforeAction = False Then
                'If pVal.FormMode = SAPbouiCOM.BoFormMode.fm_ADD_MODE And pVal.Before_Action = False And pVal.InnerEvent = False Then
                'punitha
                Try
                    oForm = SBO_Application.Forms.Item("AI_ROUTEMASTER")
                    oEdit = oForm.Items.Item("34").Specific
                    oEdit.String = Format(Now.Date, "dd/MM/yy")
                    oEdit = oForm.Items.Item("4").Specific
                    oEdit.String = Format(Now.Date, "dd/MM/yy")
                    oEdit = oForm.Items.Item("71").Specific
                    oEdit.String = Format(DateTime.Now, "ddMMyyyyhhmmssffff")
                Catch ex As Exception

                End Try
                Try
                    oForm = SBO_Application.Forms.Item("AI_ATTENDANCE")
                    oEdit = oForm.Items.Item("15").Specific
                    oEdit.String = Format(Now.Date, "dd/MM/yy")
                    oEdit = oForm.Items.Item("6").Specific
                    oEdit.String = Format(Now.Date, "dd/MM/yy")
                Catch ex As Exception

                End Try

                Try
                    oForm = SBO_Application.Forms.Item("AI_GROUPING")
                    oEdit = oForm.Items.Item("6").Specific
                    oEdit.String = Format(Now.Date, "dd/MM/yy")
                    oEdit = oForm.Items.Item("23").Specific
                    oEdit.String = Format(Now.Date, "dd/MM/yy")
                Catch ex As Exception

                End Try
                Try
                    oForm = SBO_Application.Forms.Item("AI_ACTIVITY")
                    oEdit = oForm.Items.Item("6").Specific
                    oEdit.String = Format(Now.Date, "dd/MM/yy")
                    oEdit = oForm.Items.Item("17").Specific
                    oEdit.String = Format(Now.Date, "dd/MM/yy")
                Catch ex As Exception

                End Try
                ' End If
            End If
        Catch ex As Exception

        End Try

    End Sub
    Public Sub RoutePlan_Report()
        oForm = SBO_Application.Forms.Item("NewTrans")
        Dim Sqlstr As String = ""
        Dim FPet As String = ""
        Dim TPet As String = ""
        Dim FDt As String = ""
        Dim TDt As String = ""
        Dim Type As String = ""
        oEdit = oForm.Items.Item("4").Specific
        If oEdit.String = "" Then
            FPet = "%"
        Else
            FPet = oEdit.String
        End If

        oEdit = oForm.Items.Item("8").Specific
        Dim yr As String
        If oEdit.String = "" Then
            yr = "2000"
            FDt = yr & "-01-01"
        Else
            FDt = oEdit.Value
        End If

        oEdit = oForm.Items.Item("10").Specific
        If oEdit.String = "" Then
            yr = Now.Year.ToString
            TDt = yr & "-12-31"
        Else
            TDt = oEdit.Value
        End If
        Try
            oCombo = oForm.Items.Item("12").Specific
            Type = oCombo.Selected.Value
            If Type = "All" Then
                Type = "%"
            End If
        Catch ex As Exception
        End Try

        Try
            SBO_Application.StatusBar.SetText("Retrieving Data", SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Success)
            Sqlstr = "SELECT T0.[U_AI_PetCode], T0.[U_AI_PetName], T0.[U_AI_Date], T0.[U_AI_DriverName], T0.[U_AI_TransType], T0.[U_AI_Comments] FROM [dbo].[@AI_BOARDTRANS]  T0 WHERE  T0.[U_AI_PetCode]  like '" & FPet & "' and  T0.[U_AI_TransType] like '" & Type & "'  and T0.[U_AI_Date]  between '" & FDt & "' and '" & TDt & "'  ORDER BY  T0.[U_AI_Date],T0.[U_AI_PetName],T0.[U_AI_TransType]"
            'T0.[U_AI_NewBTD ]= 'Yes' and  
            Dim frm As MY_Report
            frm = New MY_Report
            frm.NewBording_Report_Report(Sqlstr, Ocompany)
            frm.Text = "New Transport and New Boarding Report"
            frm.TopMost = True
            frm.Activate()
            frm.ShowDialog()
        Catch ex As Exception
        End Try

    End Sub
End Class
